package com.fortune.controller;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.constants.enums.DRoleType;
import com.fortune.dto.excution.DRoleExcution;
import com.fortune.dto.in.dRole.DRoleAddDto;
import com.fortune.dto.in.dRole.DRoleEditDto;
import com.fortune.dto.in.dRole.DRoleListDto;
import com.fortune.entity.DPosition;
import com.fortune.entity.DUserInfo;
import com.fortune.enums.DRoleEnum;
import com.fortune.service.DRoleService;
import com.fortune.vo.dRole.DRoleB;
import com.fortune.vo.dRole.DRoleDetail;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 
 * @author ran
 *
 */
@Api(value = "dRole", description = "经销商岗位相关", tags = {"经销商岗位相关接口"})
@RestController
@RequestMapping("dRole")
public class DRoleController {
	@Autowired
	private DRoleService dRoleService;
	@ApiOperation(value = "岗位列表", nickname = "list", notes = "", tags={ "@曹然" })
	@ApiImplicitParams({
		@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
		@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
		@ApiImplicitParam(name="name", value="岗位名称（模糊查询）",paramType="query",dataType="string"),
		@ApiImplicitParam(name="order", value="排序依据",paramType="query",dataType="string"),
		@ApiImplicitParam(name="type", value="排序类型(0升序,1降序,默认为1)",paramType="query",dataType="Integer",allowableValues = "0,1")
	})
    @GetMapping(value = "list",produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData<DRoleB>> list(@Validated @ApiIgnore DRoleListDto listDto,HttpServletRequest request) {
		listDto.setPage((listDto.getPage()-1)*10);
		DUserInfo dUserInfo = (DUserInfo) request.getSession().getAttribute(Constants.SESSION_D_USER);
		String dId = dUserInfo.getdId();
		PagingData<DRoleB> data = dRoleService.listRoles(listDto,dId);
		return new DRoleExcution<PagingData<DRoleB>>(DRoleEnum.SUCCESS,data);
	}
	
	@ApiOperation(value = "岗位详情", nickname = "detail", notes = "", tags={ "@曹然" })
	@ApiImplicitParam(name="roleId", value="岗位id",paramType="query",dataType="string", required=true)
	@GetMapping(value = "detail",produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<DRoleDetail> detail(@Validated @NotBlank(message="岗位id不能为空") String roleId,HttpServletRequest request){
		DUserInfo dUserInfo = (DUserInfo) request.getSession().getAttribute(Constants.SESSION_D_USER);
		String dId = dUserInfo.getdId();
		DRoleDetail dRoleDetail = dRoleService.getRoleDetail(dId,roleId);
		return new DRoleExcution<DRoleDetail>(DRoleEnum.SUCCESS,dRoleDetail);
	}
	
	@ApiOperation(value = "新增岗位",nickname ="add",notes = "",tags = {"@曹然"})
	@PostMapping(value = "add",
	        produces = { MediaType.APPLICATION_JSON_VALUE },
	        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> add(@Validated @RequestBody DRoleAddDto addDto,HttpServletRequest request){
		DUserInfo dUserInfo = (DUserInfo) request.getSession().getAttribute(Constants.SESSION_D_USER);
		String dId = dUserInfo.getdId();
		DPosition dPosition = dRoleService.getPositionByMIdAndDIdAndName(addDto.getName(),dId);
		if (dPosition!=null) {
			return new DRoleExcution<Void>(DRoleEnum.ROLE_NAME_EXSIST,null);
		}
		dRoleService.insertRoleWithPermissions(addDto,dId);
		return new DRoleExcution<Void>(DRoleEnum.ADD_SUCCESS,null);
	}
	
	@ApiOperation(value = "编辑岗位",nickname ="edit",notes = "",tags = {"@曹然"})
	@PutMapping(value = "edit",
	        produces = { MediaType.APPLICATION_JSON_VALUE },
	        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(@Validated @RequestBody DRoleEditDto editDto,HttpServletRequest request){
		DUserInfo dUserInfo = (DUserInfo) request.getSession().getAttribute(Constants.SESSION_D_USER);
		String dId = dUserInfo.getdId();
		DPosition dPosition = dRoleService.getPositionByMIdAndDIdAndRoleId(dId,editDto.getRoleId());
		if (dPosition==null) {
			return new DRoleExcution<Void>(DRoleEnum.EDIT_ROLE_NOT_EXSIST,null);
		}
		if (dPosition.getdRoleType()==DRoleType.MR) {
			return new DRoleExcution<Void>(DRoleEnum.EDIT_ROLE_FORBIDDEN,null);
		}
		dPosition = dRoleService.getPositionByMIdAndDIdAndNameAndRoleId(editDto.getName(),dId,editDto.getRoleId());
		if (dPosition!=null) {
			return new DRoleExcution<Void>(DRoleEnum.ROLE_NAME_EXSIST,null);
		}
		dRoleService.updateRoleWithPermissions(editDto,dId);
		return new DRoleExcution<Void>(DRoleEnum.EDIT_SUCCESS,null);
	}
	
	@ApiOperation(value = "删除岗位",nickname ="delete",notes = "",tags = {"@曹然"})
	@ApiImplicitParam(name="roleId", value="岗位id",paramType="query",dataType="string", required=true)
	@DeleteMapping(value = "delete",
	        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(@Validated @NotBlank(message="岗位id不能为空") String roleId,HttpServletRequest request){
		DUserInfo dUserInfo = (DUserInfo) request.getSession().getAttribute(Constants.SESSION_D_USER);
		String dId = dUserInfo.getdId();
		DPosition dPosition = dRoleService.getPositionByMIdAndDIdAndRoleId(dId,roleId);
		if (dPosition==null) {
			return new DRoleExcution<Void>(DRoleEnum.DELETE_ROLE_NOT_EXSIST,null);
		}
		int result = dRoleService.findEvery(dId,roleId);
		if (result > 0) {
			return new DRoleExcution<Void>(DRoleEnum.ROLE_EXIST,null);
		}
		result = dRoleService.deleteRole(dId,roleId);
		if (result==0) {
			return new DRoleExcution<Void>(DRoleEnum.DELETE_ROLE_FAILED,null);
		}
		return new DRoleExcution<Void>(DRoleEnum.DELETE_SUCCESS,null);
	}
}
